var DateTool = function(){
	
};
DateTool.prototype.converDate = function(startDateStr, endDateStr) {
	startDateStr = startDateStr.replace(/\-/g,"/");
	var startDate = new Date(startDateStr);
	var endDate;
	if(endDateStr == null) {
		endDate = new Date();
	}else {
		 endDate = new Date(endDateStr);
	}
	var differSecond =Math.ceil((endDate.getTime() - startDate.getTime()) / 1000);
	
	if(differSecond  < 60) {
		return differSecond + "秒钟前";
	}
	var differMinute = Math.ceil(differSecond / 60);
	if(differMinute < 60) {
		return differMinute + "分钟前";
	}
	var differHour = Math.ceil(differMinute / 60);
	if(differHour < 24) {
		return differHour + "小时前";
	}
	var differDay = Math.ceil(differHour/24);
	if(differDay < 31) {
		return differDay + "天前" + this.converDouble(startDate.getHours()) + ":" + this.converDouble(startDate.getMinutes()) ;
	}
	return startDateStr;
}
DateTool.prototype.printDiffDateStr = function(startDateStr, endDateStr) {
	document.write(this.converDate(startDateStr, endDateStr));
}

/**
 * 转化为字符串
 * DateTool.format(date,"yyyy-MM-dd hh:mm:ss")
 */
DateTool.prototype.format = function(date,format)
{
	var o = {
	            "M+" : date.getMonth()+1, // month
	            "d+" : date.getDate(), // day
	            "h+" : date.getHours(), // hour
	            "m+" : date.getMinutes(), // minute
	            "s+" : date.getSeconds(), // second
	            "q+" : Math.floor((date.getMonth()+3)/3), // quarter
	            "S" : date.getMilliseconds() // millisecond
	        }
	    if(/(y+)/.test(format))
	    	format=format.replace(RegExp.$1,(date.getFullYear()+"").substr(4 - RegExp.$1.length));
	    for(var k in o)
	    	if(new RegExp("("+ k +")").test(format))
	    		format = format.replace(RegExp.$1,RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
	     return format;
}
/**
 * 
 */
DateTool.prototype.getCurrentTime = function(){
	return this.format(new Date(),'yyy-MM-dd hh:mm:ss');
}

/**
 * 将个位数的分和秒转化为两位数
 */
DateTool.prototype.converDouble = function (t) {
	if(t < 10) return '0' + t;
	return t;
}

var dateTool = new DateTool();